<?php

class Model_A2c extends Ac_Model
{
	protected $_ch;
	protected $_url;
	protected $_params;
	
	protected $_data = array(
		'result' => ''
	);

    public function __construct()
	{

    }
    
    public function init()
    {
    	$this->_ch = null;
    	//$this->_url = A2C_API_HOSTNAME;
    	$this->_url = 'http://beijing.api.luke.beta1.maozhao.com';
    	$this->_params = array();
    	$this->result = null;
    }
    
    protected function postUserLogin($username, $password)
    {
    	$this->_url .= "/1/user/login";
    	$this->_params = array(
    		'username' => $username,
    		'password' => $password
    	);
    	$this->cPost();
    }
    
	protected function postUserSignup($username, $email, $password, $phone, $newsletter = 0)
    {
    	$this->_url .= "/1/user/signup";
    	$this->_params = array(
    		'username' => $username,
    		'email' => $email,
    		'password' => $password,
    		'phone' => $phone,
    		'newsletter' => $newsletter
    	);
    	$this->cPost();
    }
    
    protected function postListing($lang, $title, $description, $category, $userid, $sessionid, $district, $photos = null, $price = null, $privatekey = null, $url = null, $lat = null, $lon = null, $tag = null)
    {
    	$this->_url .= "/1/listing";
    	$this->_params = array(
    		'lang' => $lang,
    		'title' => $title,
    		'description' => $description,
    		'category' => $category,
    		'userid' => $userid,
    		'sessionid' => $sessionid,
    		'photos' => $photos,
    		'price' => $price,
    		'privatekey' => $privatekey,
    		'district' => $district,
    		'url' => $url,
    		'lat' => $lat,
    		'lon' => $lon,
    		'tag' => $tag
    	);
    	$this->cPost();
    }
    
    protected function getTopCategory()
    {
    	$this->_url .= "/1/category";
    	$this->cGet();
    }
    
    protected function getSpecificCategory($id, $pn = null)
    {
    	$this->_url .= "/1/category";
    	$this->_params = array($id, $pn);
    	$this->cGet();
    }
    
    protected function getSpecificListing($id)
    {
    	$this->_url .= "/1/listing";
    	$this->_params = array($id);
    	$this->cGet();
    }
    
    
    
    
    
    protected function cGet()
    {
    	$this->_params = array_filter($this->_params);
    	$this->_ch = curl_init();
		curl_setopt($this->_ch, CURLOPT_URL, $this->_url . '/' . implode('/', $this->_params));
		curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, false);
		$this->result = curl_exec($this->_ch);
		curl_close($this->_ch);
    }

    protected function cPost()
    {
    	$this->_params = array_filter($this->_params);
    	$this->_ch = curl_init();
		curl_setopt($this->_ch, CURLOPT_URL, $this->_url);
		curl_setopt($this->_ch, CURLOPT_POST, 1);
		curl_setopt($this->_ch, CURLOPT_POSTFIELDS, http_build_query($this->_params, '', '&'));
		curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->_ch, CURLOPT_SSL_VERIFYPEER, false);
		$this->result = curl_exec($this->_ch);
		curl_close($this->_ch);
    }

    public function __call($name, $arguments)
    {
    	if(method_exists($this, $name)){
    		$this->init();
    		call_user_func_array(array(&$this, $name), $arguments);
    		try{
	    		$this->result = json_decode($this->result);
	    		return $this->result;
    		}catch (Exception $e){
//    			$error = $this->result;
//    			$this->result = new stdClass();
//    			$this->result->error = $error;
//    			return $this->result;
    		}
    	}else{
    		//return parent::_call($name, $arguments);
    	}
    }
}
